<?php

namespace app\admin\controller;

class CompanyBase
{
    public function index()
    {
        $query = \app\model\Company::field(['id', 'name', 'created_at'])->order('id', 'desc');

        if ($name = input('name')) {
            $query->whereLike('name', '%' . $name . '%');
        }

        $models = $query->select();

        return json(['code' => 20000, 'data' => ['companies' => $models]]);
    }

    public function delete()
    {
        $model = \app\model\Company::destroy(input('id'));

        return json(['code' => 20000, 'msg' => '移除成功', 'data' => $model]);
    }

    public function update()
    {
        $model = \app\model\Company::update(input());

        return json(['code' => 20000, 'msg' => '更新成功', 'data' => $model]);
    }

    public function create()
    {
        $param = request()->param();

        $model = \app\model\Company::create($param);

        if (!$model) {
            return json(['code' => 40400, 'msg' => '新增失败']);
        }

        return json(['code' => 20000, 'msg' => '新增成功', 'data' => $model]);
    }

    public function detail()
    {
        $query = \app\model\Company::field(['id', 'name']);

        if ($id = input('id')) {
            $query->where('id', $id);
        }

        $model = $query->find();

        return json(['code' => 20000, 'data' => ['company' => $model]]);
    }
}
